package com.qczl.pme.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qczl.pme.dao.CameraDao;
import com.qczl.pme.dao.SewiseImageDao;
import com.qczl.pme.entity.PMECamera;
import com.qczl.pme.entity.PMESewiseImage;
import com.qczl.pme.service.ScreenService;
import com.qczl.util.DateUtil;
import com.qczl.util.DownLoadUtil;

/**
 * 摄像头视频业务逻辑层实现类
 * 
 * @author SongR
 *
 */
@Service
public class ScreenServiceImpl implements ScreenService {

	@Autowired
	CameraDao cameradao;

	@Autowired
	SewiseImageDao sewiseimagedao;

	/**
	 * 实现视频截图
	 */
	@Override
	public boolean take(String cameraId, int type) throws Exception {
		// TODO 视频截图
		boolean isTrue = false;
		PMECamera camera = cameradao.get(PMECamera.class, new Long(cameraId));
		String path = System.getProperty("catalina.home");
		if (null != camera) {
			if (null != camera.getUrl() && !"".equals(camera.getUrl())) {
				if (null != camera.getSewise()) {
					String imageName = DateUtil.dateToStamp(DateUtil.dataToStr(new Date(), "yyyy-MM-dd HH:mm:ss"))
							+ ".jpeg";
					String paths = path + "/webapps/resource/sewise/image/" + camera.getStreamId();
					String port = "";
					if (null != camera.getSewise().getProt3() && !"".equals(camera.getSewise().getProt3())) {
						port = ":" + camera.getSewise().getProt3();
					}
					DownLoadUtil.download("http://" + camera.getSewise().getIp() + port + "/snaptshot/"
							+ camera.getStreamId() + ".jpeg", imageName, paths);
					PMESewiseImage image = new PMESewiseImage();
					image.setCameraid(camera.getId());
					image.setDate(new Date());
					image.setPath("/resource/sewise/image/" + camera.getStreamId() + "/" + imageName);
					image.setTypeid(new Long(type));
					image.setState(0);
					sewiseimagedao.save(image);
					isTrue = true;
				}
			}
		}
		return isTrue;
	}

}
